package com.skyc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.skyc.entity.Clothes;
import com.skyc.entity.User;
import com.skyc.service.collectionService;
import com.skyc.service.liuLanService;
import com.skyc.service.impl.collectionServiceImpl;
import com.skyc.service.impl.liulanServiceImpl;

/**
 * Servlet implementation class toindexServlet
 */
@WebServlet("/toindexServlet")
public class toindexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		liuLanService ls = new liulanServiceImpl();
		User user = (User) session.getAttribute("user"); 
		
		List<Clothes> BKList = ls.BKqueryByID();
		List<Clothes> menList = ls.NZqueryByID("男");
		List<Clothes> womenList = ls.NZqueryByID("女");
		
		for (Clothes c : menList) {
			c.setCimgurl(c.getCimgurl().substring(0, c.getCimgurl().indexOf('/')));
		}
		for (Clothes c : BKList) {
			c.setCimgurl(c.getCimgurl().substring(0, c.getCimgurl().indexOf('/')));
		}
		for (Clothes c : womenList) {
			c.setCimgurl(c.getCimgurl().substring(0, c.getCimgurl().indexOf('/')));
		}
		collectionService ce = new collectionServiceImpl();

		List<String> wocc = new ArrayList<String>();
		List<String> cc = new ArrayList<String>();
		List<String> bkcc = new ArrayList<String>();
		if (user != null) {
			session.setAttribute("user", user);
			List<Clothes> uc = ce.ucollection(user);
			for (Clothes n : menList) {
				if (uc.size() != 0) {
					for (Clothes n1 : uc) {
						if (n.getCcid() == n1.getCcid()) {
							cc.add("取消收藏");
						} else {
							cc.add("收藏");
						}
					}
				} else {
					cc.add("收藏");
					System.out.println(111);
				}
			}

			for (Clothes n : BKList) {
				if (uc.size() != 0) {
					for (Clothes n1 : uc) {
						if (n.getCcid() == n1.getCcid()) {
							bkcc.add("取消收藏");
						} else {
							bkcc.add("收藏");
						}
					}
				} else {
					bkcc.add("收藏");
					System.out.println(222);
				}
			}

			for (Clothes n : womenList) {
				if (uc.size() != 0) {
					for (Clothes n1 : uc) {
						if (n.getCcid() == n1.getCcid()) {
							wocc.add("取消收藏");
						} else {
							wocc.add("收藏");
						}
					}
				} else {
					wocc.add("收藏");
					System.out.println(333);
				}
			}
		} else {
			session.setAttribute("user", null);
			for (Clothes n : menList) {
				cc.add("收藏");
			}
			for (Clothes n : BKList) {
				bkcc.add("收藏");
			}
			for (Clothes n : womenList) {
				wocc.add("收藏");
			}
		}

		session.setAttribute("bkcc", bkcc);
		session.setAttribute("wocc", wocc);
		session.setAttribute("cc", cc);
		session.setAttribute("BKList", BKList);
		session.setAttribute("menList", menList);
		session.setAttribute("womenList", womenList);
		request.getRequestDispatcher("index.jsp").forward(request, response);

	}
}